/* eslint-disable react/no-unused-state */
import { Component } from 'react';
import { View } from '@tarojs/components';
import NavBar from '@/components/NavBar/index';
import { globalStore } from '@/store';
import { observer } from 'mobx-react';
import Taro from '@tarojs/taro';
import EmptyContent from '@/components/EmptyContent';
import { getToken, checkAuth } from '@/utils/authUtil';
import {
  getHomeTopicList,
  getHomeBanners,
  getHomePopups,
  getMiaoshaList,
  getHomeProductList,
} from '@/service/apis/home';
// import { getProductCateList } from '@/service/apis/product/index';
import Header from './components/Header/index';
import Banner from './components/Banner/index';
import Miaosha from './components/Miaosha/index';
import Service from './components/Service/index';
import GoodsList from './components/GoodsList/index';
import Popup from './components/Popup/index';
import './index.scss';

class Index extends Component {
  state = {
    popupShow: false,
    jumped: false,
    // store_id: 0,
    // shopInfo: {},
    activityList: [],
    bannerList: [],
    popupList: [],
    miaoshaList: [],
    goodsList: {},
  };
  componentDidMount = async () => {
    Taro.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
    });
    // this.getHomeInfo();
  };

  componentDidShow() {
    const locationAuth = Taro.getStorageSync('LOCATION_AUTH');
    if (!locationAuth) {
      this.getHomeInfo(false);
    }
    this.getHomeInfo(true);
  }

  config = {
    navigationBarTitleText: '首页',
    navigationBarBackgroundColor: '#000',
    navigationBarTextStyle: 'white',
  };

  getHomeTopicListReq = async () => {
    try {
      const topicData = await getHomeTopicList();
      this.setState({
        activityList: topicData.data.topic_list,
      });
    } catch (err) {
      console.log('err', err);
      Taro.showToast({
        title: err.message,
        icon: 'none',
      });
    }
  };
  getHomeBannersReq = async () => {
    try {
      const bannerData = await getHomeBanners();
      this.setState({
        bannerList: bannerData.data.banner_list,
      });
    } catch (err) {
      console.log('err', err);
      Taro.showToast({
        title: err.message,
        icon: 'none',
      });
    }
  };
  getMiaoshaListReq = async () => {
    try {
      const miaoshaList = await getMiaoshaList();
      this.setState({
        miaoshaList: miaoshaList.data.kill_list,
      });
    } catch (err) {
      console.log('err', err);
      Taro.showToast({
        title: err.message,
        icon: 'none',
      });
    }
  };
  getHomeProductListReq = async () => {
    try {
      const goodsList = await getHomeProductList({
        store_name: globalStore.currentShop.store_name,
      });
      this.setState({
        goodsList: goodsList.data.goods_list,
      });
    } catch (err) {
      console.log('err', err);
      Taro.showToast({
        title: err.message,
        icon: 'none',
      });
    }
  };
  getHomePopupsReq = async () => {
    try {
      const popupList = await getHomePopups();
      this.setState({
        popupList: popupList.data.pop_list,
        popupShow: popupList.data.pop_list.length > 0,
      });
    } catch (err) {
      console.log('err', err);
      Taro.showToast({
        title: err.message,
        icon: 'none',
      });
    }
  };

  // flag: 是否授权过定位
  getHomeInfo = async (flag?: boolean) => {
    const { jumped } = this.state;
    await globalStore.getCurrentShop(flag);
    const querys = Taro.getCurrentInstance().router?.params;
    if (querys?.jump && flag && !jumped) {
      this.jumpToPage(querys?.jump);
      this.setState({ jumped: true });
    }
    this.getHomeTopicListReq();
    this.getHomeBannersReq();
    this.getMiaoshaListReq();
    this.getHomeProductListReq();
    if (getToken()) {
      this.getHomePopupsReq();
    }
  };

  jumpToPage = (type: string) => {
    if (type === 'shopList') {
      Taro.navigateTo({
        url: '/subPages/shop/list',
      });
      return;
    }
    if (type === 'appointment') {
      Taro.switchTab({
        url: '/pages/appointment/index',
      });
      return;
    }
    if (type === 'activity') {
      checkAuth(() => {
        Taro.navigateTo({
          url: '/pages/activity/index',
        });
      });
      return;
    }
  };

  closePopup = () => {
    this.setState({
      popupShow: false,
    });
  };

  render() {
    const { popupShow, activityList, bannerList, popupList, miaoshaList, goodsList } = this.state;
    console.log('show', this.props);
    return (
      <View className="home-wrap">
        <NavBar
          showBackBtn={false}
          title="趣驿站"
          style={{ color: '#fff', background: '#f82256' }}
        />
        <Header shopInfo={globalStore.currentShop} activityList={activityList} />
        {bannerList.length > 0 && <Banner bannerList={bannerList} />}
        {miaoshaList.length > 0 && <Miaosha miaoshaList={miaoshaList} />}
        <Service />
        {goodsList.length !== 0 ? (
          <GoodsList goodsList={goodsList} />
        ) : (
          <EmptyContent offsetTop={50} title="暂无相关商品" />
        )}
        {popupList.length > 0 && (
          <Popup show={popupShow} popupList={popupList} onClose={this.closePopup} />
        )}
      </View>
    );
  }
}
export default observer(Index);
